'use strict';

/* Controllers */

app
// Flot Chart controller
    .controller('userCtrl', ['$scope', '$http', '$stateParams','$rootScope','$filter','$location', function ($scope, $http, $stateParams,$rootScope,$filter,$location) {
    	$scope.info={};
    	$scope.info.userName=getUserAccount();
    	$scope.info.isButton=true;
    	$scope.info.oldPassword="";
    	$scope.info.newPassword="";
    	 //清空header数据
        $rootScope.clean($scope.app)
        //初始化header数据
        $scope.app.page = {};
        $scope.app.page.name="修改密码"

        $scope.rolelist = {};
        $.ajax({
            type: "GET",
            url: beetle_auth_url + '/role/?token_id=' + getTokenId(),
            async: false,
            success: function (data) {
                $scope.rolelist = data.data;

            }
        });
        $scope.save=function(){
        	var obj=[];
        	angular.forEach($scope.rolelist, function(role){
        		angular.forEach(getUserRole(), function(oldRole){
        			if(role.role_name==oldRole){
        				obj.push(role.role_id)
        			}
        		})
        	})
        	$http.get(beetle_auth_url+"/user/update?token_id="+getTokenId()+"&account="+$scope.info.userName+"&update_pass=true&role_list="+JSON.stringify(obj)+"&id="+getUserId()+"&new_password="+$scope.info.newPassword+"&old_password="+$scope.info.oldPassword+"&realname="+getUserName()).success(function(data){
        		if (data.success == true) {
                    $scope.back();
                } else {
                    alert(data.msg);
                }
        	})
        }
        $scope.back=function(){
        	window.location.href = '#/app/dashboard-v1';
        }
        

    }])